/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The software in this package is published under the terms of the GPL v2.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE-GPL.txt file.
 */

package org.cemantika.util;

import org.cemantika.exception.CemantikaConfigurationException;
import org.cemantika.management.ContextualBehavior;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * SpringUtil represents the initialization of spring to build the application environment. 
 *
 * @author andersonfellipe
 */
@SuppressWarnings("restriction")
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    
    public static void createConfiguration(String... appContextXmlPaths) {
        try {
            applicationContext = new ClassPathXmlApplicationContext(appContextXmlPaths);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Returns the {@link ContextualBehavior} configured with Spring.
     * 
     * @param contextualBehaviorId the id of the {@link ContextualBehavior}.
     * @return the object og the {@link ContextualBehavior}.
     */
    public static ContextualBehavior getBeanContextualBehavior(String contextualBehaviorId) throws CemantikaConfigurationException {
        if (!isSpringConfigured()) {
            throw new CemantikaConfigurationException();
        }
        return (ContextualBehavior) applicationContext.getBean(contextualBehaviorId, ContextualBehavior.class);
    }
    
    /**
     * Represents the test to verify if the Spring is configured.
     * 
     * @return true if the spring is configured, and false otherwise.
     */
    public static boolean isSpringConfigured() {
        return (applicationContext != null);
    }
    
    /**
     * Represents the halt of the spring framework
     */
    public static void terminate() {
        ((ClassPathXmlApplicationContext) applicationContext).close(); 
    }

    /* (non-Javadoc)
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext = applicationContext;
    }

    /**
     * Get the applicationContext value.
     *
     * @return the applicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

}
